<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/6/26
 * Time: 10:41
 * 销售员控制器
 */
class Kdtsalesman extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'salesman';
        $this->_list_where = array();
        $this->_action_list = array(
            'EDIT',
            array('controller'=>'','function'=>'','label'=>'优惠券')
        );//EDIT,DEL,array('controller'=>'admin/commanswer/','function'=>'showlist','label'=>'查看回答')
        $this->_controller = 'admin/salesman/kdtsalesman/';
        parent::__construct();
    }

    public function showList($page = 1)
    {
        $this->load->model('kdtsalesman_model');
        $coupons = $this->kdtsalesman_model->getAllCoupons();
        $slstypes = $this->kdtsalesman_model->getSlsType();
        $get = $this->input->get();
        if (!empty($get)){
            $where = array();
            $start = !empty($get['start'])?strtotime(trim($get['start'])):'';
            $end = !empty($get['end'])?strtotime(trim($get['end'])):'';
            $name = !empty($get['fname'])?trim($get['fname']):'';
            $username = !empty($get['fusername'])?trim($get['fusername']):'';
            $idcode = !empty($get['fidcode'])?trim($get['fidcode']):'';
            $phone = !empty($get['fphone'])?trim($get['fphone']):'';
            $invi = !empty($get['finvi'])?trim($get['finvi']):'';
            $type = !empty($get['ftype'])?trim($get['ftype']):'';
            if (!empty($start)) $where['s.created >'] = $start;
            if (!empty($end)) $where['s.created <'] = $end;
            if (!empty($name)) $where['s.name LIKE']= '%'.$name.'%';
            if (!empty($username)) $where['u.userName LIKE']= '%'.$username.'%';
            if (!empty($idcode)) $where['s.idcode LIKE']= '%'.$idcode.'%';
            if (!empty($phone)) $where['s.phone LIKE']= '%'.$phone.'%';
            if (!empty($invi)) $where['s.invifrom LIKE']= '%'.$invi.'%';
            if (!empty($type)) $where['s.type =']= $type;
            $this->_list_where = $where;
            $filter = $this->load->view('admin/form/filter/kdtsalesmanfilter',array('controller'=>site_url($this->_controller),'start'=>$get['start'],
                'end'=>$get['end'],'fname'=>$name,'fusername'=>$username,'fidcode'=>$idcode,'fphone'=>$phone,'finvi'=>$invi,'ftype'=>$type,
                'coupons'=>$coupons,'slstypes'=>$slstypes),true);
        }else{
            $filter = $this->load->view('admin/form/filter/kdtsalesmanfilter',array('controller'=>site_url($this->_controller),
                'coupons'=>$coupons,'slstypes'=>$slstypes),true);
        }

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'filter'=>$filter);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }

    public function getList($offset = 0,$limit = 0)
    {
        $header = array("销售员","微信昵称","销售员ID","电话","上线","渠道","分享收益","已提现","加入时间","操作");
        $this->load->model('kdtsalesman_model');
        $list = $this->kdtsalesman_model->getSalesmanListData($this->_controller,$this->_list_where,$limit,$offset);
        $res = array('headers'=>$header,'list'=>$list);
        return $res;
    }

    public function getPagination($uri = '')
    {
        $this->db->from('mod_salesman as s')->join('mod_user as u','u.uid=s.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $total = $this->db->count_all_results();

        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['suffix'] = '?'.$_SERVER['QUERY_STRING'];
        $config['first_url'] = $config['base_url'].'1?'.$_SERVER['QUERY_STRING'];
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

    public function synCoupon()
    {
        $this->load->model('kdt_api_model');
        $res = $this->kdt_api_model->getCouponList();
        if ($res!==false) {
            $this->load->model('kdtsalesman_model');
            $this->kdtsalesman_model->synCoupon($res);
        }
        redirect(site_url($this->_controller.'showList/1'));
        exit();
    }

    public function test()
    {
        $this->load->model('kdt_api_model');
        $res = $this->kdt_api_model->getCouponList();
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
    }

    public function update($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            $this->_update_data = $post;
            parent::update($id);
        }
    }

    public function addBatchCoupons()
    {
        $coupontype = $this->input->get('coupontype');
        $coupons = $this->input->get('coupons');
        if (isset($coupontype)&&!empty($coupons)) $coupontype = trim($coupontype);
        else{
            echo json_encode(array('code'=>0,'msg'=>'lack params'));
            exit();
        }
        if (isset($coupons)&&!empty($coupons)) {
            $coupons = trim($coupons,'|');
            $coupons = explode('|',$coupons);
        }else{
            echo json_encode(array('code'=>0,'msg'=>'lack params'));
            exit();
        }

        $this->load->model('kdtsalesman_model');
        $this->kdtsalesman_model->addBatchCoupons($coupontype,$coupons);

        echo json_encode(array('code'=>1,'msg'=>'success','data'=>array()));
        exit();
    }

    public function editCoupons()
    {
        $id = $this->input->get('id');
        $coupons = $this->input->get('coupons');
        if (!isset($id)||!is_numeric($id)){
            echo json_encode(array('code'=>0,'msg'=>'lack params'));
            exit();
        }
        $this->load->model('kdtsalesman_model');
        if (isset($coupons)){
            $coupons = trim($coupons,'|');
            $coupons = explode('|',$coupons);
            $this->kdtsalesman_model->editCoupons($id,$coupons);
            echo json_encode(array('code'=>1,'msg'=>'success','data'=>array()));
            exit();
        }else{
            $res=$this->kdtsalesman_model->getEditCoupons($id);
            echo json_encode(array('code'=>1,'msg'=>'success','data'=>$res));
            exit();
        }
    }
    public function afterupdate($pkey,$pkeyval){}

    public function create(){}

    public function aftercreate($pkeyval){}

    public function delete($id = 0,$externalid = 0){}

    public function afterdelete($pkey,$pkeyval){}
}